<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pembul extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->model('ktig');
	}
	public function index()
	{
		$this->load->helper('url');
		$this->load->view('header');
		
		$data['RTLS1']=$this->ktig->pembul_status('S1');
		$data['RTLS2']=$this->ktig->pembul_status('S2');
		$data['RTLS3']=$this->ktig->pembul_status('S3');
		$data['RTLS4']=$this->ktig->pembul_status('S4');
		$data['RTLS5']=$this->ktig->pembul_status('S5');
		
		$data['RTLK1']=$this->ktig->pembul_status('K1');
		$data['RTLK2']=$this->ktig->pembul_status('K2');
		$data['RTLK3']=$this->ktig->pembul_status('K3');
		$data['RTLK4']=$this->ktig->pembul_status('K4');
		$data['RTLK5']=$this->ktig->pembul_status('K5');
		
		$data['RTLG1']=$this->ktig->pembul_status('G1');
		$data['RTLG2']=$this->ktig->pembul_status('G2');
		$data['RTLG3']=$this->ktig->pembul_status('G3');
		$data['RTLG4']=$this->ktig->pembul_status('G4');
		$data['RTLG5']=$this->ktig->pembul_status('G5');
		
		$this->ktig->is_logged_in();
		if($this->session->userdata('role')==1)
		{
			if(date('d')<11) { $this->load->view('v_pembul',$data); }
		}
		else if ($this->session->userdata('role')==5)
		{
			$this->load->view('v_pembul',$data);
		}
		else { redirect('land'); }
	}
	public function input_form()
	{
		$this->load->helper('url');
		
		/**********Update**************/
		/*$data_update=array(
			'RTLS1'=>2,
			'RTLS2'=>2,
			'RTLS3'=>2,
			'RTLS4'=>2,
			'RTLS5'=>2,
			
			'RTLK1'=>2,
			'RTLK2'=>2,
			'RTLK3'=>2,
			'RTLK4'=>2,
			'RTLK5'=>2,
			
			'RTLG1'=>2,
			'RTLG2'=>2,
			'RTLG3'=>2,
			'RTLG4'=>2,
			'RTLG5'=>2
		  );*/
		  $data_update=array(
			'STATUS_PROGRES'=>2
		  );
		
		$this->db->where('JENIS_FORM',1);
		$this->db->update('laporan_bulanan',$data_update);
		/******************************/
		
		$tanggal_input = $this->input->post('tanggal');
		$tanggal = date("Y-m-d", strtotime($tanggal_input));
		$jam = date("H:i:s", strtotime($tanggal_input));
		//$date = date("Y-m-d");
		$date = null;
		
		if($this->input->post('pengawas'))
		{
			if($this->input->post('skertaris')==1) { $status_skertaris=3; }
			else { $status_skertaris=2; }
		}
		else { $status_skertaris=1; }
		
		$status_progres=1;
		
		$data=array(
			'JENIS_FORM'=>1,
			'BAGIAN'=>$this->input->post('bagian'),
			'DEPARTEMEN'=>$this->input->post('departemen'),
			'TANGGAL'=>$tanggal,
			
			'S1'=>$this->input->post('sta1'),
			'TBS1'=>$this->input->post('tbk1'),
			'TLS1'=>$this->input->post('tar1'),
			'RTLS1'=>$this->input->post('s1'),
			'TPS1'=>$date,
			'S2'=>$this->input->post('sta2'),
			'TBS2'=>$this->input->post('tbk2'),
			'TLS2'=>$this->input->post('tar2'),
			'RTLS2'=>$this->input->post('s2'),
			'TPS2'=>$date,
			'S3'=>$this->input->post('sta3'),
			'TBS3'=>$this->input->post('tbk3'),
			'TLS3'=>$this->input->post('tar3'),
			'RTLS3'=>$this->input->post('s3'),
			'TPS3'=>$date,
			'S4'=>$this->input->post('sta4'),
			'TBS4'=>$this->input->post('tbk4'),
			'TLS4'=>$this->input->post('tar4'),
			'RTLS4'=>$this->input->post('s4'),
			'TPS4'=>$date,
			'S5'=>$this->input->post('sta5'),
			'TBS5'=>$this->input->post('tbk5'),
			'TLS5'=>$this->input->post('tar5'),
			'RTLS5'=>$this->input->post('s5'),
			'TPS5'=>$date,
			
			'K1'=>$this->input->post('kta1'),
			'TBK1'=>$this->input->post('rt6'),
			'TLK1'=>$this->input->post('tar6'),
			'RTLK1'=>$this->input->post('k1'),
			'TPK1'=>$date,
			'K2'=>$this->input->post('kta2'),
			'TBK2'=>$this->input->post('rt7'),
			'TLK2'=>$this->input->post('tar7'),
			'RTLK2'=>$this->input->post('k2'),
			'TPK2'=>$date,
			'K3'=>$this->input->post('kta3'),
			'TBK3'=>$this->input->post('rt8'),
			'TLK3'=>$this->input->post('tar8'),
			'RTLK3'=>$this->input->post('k3'),
			'TPK3'=>$date,
			'K4'=>$this->input->post('kta4'),
			'TBK4'=>$this->input->post('rt9'),
			'TLK4'=>$this->input->post('tar9'),
			'RTLK4'=>$this->input->post('k4'),
			'TPK4'=>$date,
			'K5'=>$this->input->post('kta5'),
			'TBK5'=>$this->input->post('rt10'),
			'TLK5'=>$this->input->post('tar10'),
			'RTLK5'=>$this->input->post('k5'),
			'TPK5'=>$date,
			
			'G1'=>$this->input->post('lk1'),
			'TBG1'=>$this->input->post('rt11'),
			'TLG1'=>$this->input->post('tar11'),
			'RTLG1'=>$this->input->post('l1'),
			'TPG1'=>$date,
			'G2'=>$this->input->post('lk2'),
			'TBG2'=>$this->input->post('rt12'),
			'TLG2'=>$this->input->post('tar12'),
			'RTLG2'=>$this->input->post('l2'),
			'TPG2'=>$date,
			'G3'=>$this->input->post('lk3'),
			'TBG3'=>$this->input->post('rt13'),
			'TLG3'=>$this->input->post('tar13'),
			'RTLG3'=>$this->input->post('l3'),
			'TPG3'=>$date,
			'G4'=>$this->input->post('lk4'),
			'TBG4'=>$this->input->post('rt14'),
			'TLG4'=>$this->input->post('tar14'),
			'RTLG4'=>$this->input->post('l4'),
			'TPG4'=>$date,
			'G5'=>$this->input->post('lk5'),
			'TBG5'=>$this->input->post('rt15'),
			'TLG5'=>$this->input->post('tar15'),
			'RTLG5'=>$this->input->post('l5'),
			'TPG5'=>$date,
			
			'ANGGOTA'=>$this->input->post('nama1'),
			'ANGGOTA2'=>$this->input->post('nama2'),
			'PENGAWAS'=>$this->input->post('pengawas'),
			'STATUS'=>$this->input->post('skertaris'),
			'STATUS_PROGRES'=>$status_progres
		);
		
		/**********Insert**************/
		$this->db->insert('laporan_bulanan',$data);
		/******************************/
		
		/*********Email Alert**********/
		if($this->input->post('tbk1')>2 or $this->input->post('tbk2')>2 or $this->input->post('tbk3')>2 or $this->input->post('tbk4')>2 or $this->input->post('tbk5')>2 or $this->input->post('rt6')>2 or $this->input->post('rt7')>2 or $this->input->post('rt8')>2 or $this->input->post('rt9')>2 or $this->input->post('rt10')>2 or $this->input->post('rt11')>2 or $this->input->post('rt12')>2 or $this->input->post('rt13')>2 or $this->input->post('rt14')>2 or $this->input->post('rt15')>2)
		{
			//$this->load->library('email'); 
			$config = Array(
				'protocol' 		=> 'smtp',
				'smtp_host' 	=> 'ssl://smtp.googlemail.com',
				'smtp_port' 	=> 465,
				'smtp_user' 	=> 'k3teamit@gmail.com',
				'smtp_pass' 	=> 'katiga87',
				'mailtype'  	=> 'html', 
				'smtp_timeout'	=> 30,
				'charset'   	=> 'iso-8859-1'
				);

			$this->load->library('email', $config);
			$this->email->set_newline("\r\n");
			$this->email->from('k3teamit@gmail.com','Allen'); 
			$this->email->to("Igha3.moet@gmail.com");
			$this->email->subject('K3 Alert!'); 
			$this->email->message("Terdapat Potensi Bahaya Tinggi"); 
			$this->email->send();
		}
		/******************************/
		
		redirect('pembul/lihat');
	}
	public function lihat()
	{
		$this->load->helper('url');
		$this->load->view('header');
		
		$data['datalist'] = $this->ktig->list_pembul();
		
		$this->load->view('v_pembul_lihat',$data);
	}
	public function hapus($id)
	{
		$this->load->helper('url');
		$this->ktig->hapus_pembul($id);
		redirect('pembul/lihat');
	}
	public function ubah($id)
	{
		$this->load->helper('url');
		$this->load->view('header');
		if($this->input->post('submit'))
		{
			$this->ktig->update_pembul($id);
			redirect('pembul/lihat');
		}
		$data['hasil']=$this->ktig->data_pembul($id);
		$this->load->view('v_pembul_ubah',$data);
	}
	public function detail($id)
	{
		$this->load->helper('url');
		$this->load->view('header');
		if($this->input->post('submit'))
		{
			redirect('pembul/lihat');
		}
		
		$data['hasil']=$this->ktig->data_pembul($id);
		$this->load->view('v_pembul_detail',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */